#![allow(unused_imports,unused_assignments)]
use serde::{Deserialize, Serialize};
use sqlx::mysql::MySqlRow;
use sqlx::{FromRow, Row};
use sqlx::Error;
use crate::domain::count_num::get_image_base64;
use crate::domain::count_num::get_i64;
use crate::domain::count_num::get_i32;
use crate::domain::count_num::get_f64;
use crate::domain::count_num::get_f32;
use crate::domain::count_num::get_bool;
use crate::domain::count_num::get_string;
use crate::domain::count_num::get_dropdown;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct UserBean {
	pub id: i64,
	pub user_name: String,
	pub active: bool,
	pub first_name: String,
	pub last_name: String,
	pub email: String,
	pub isadmin: bool,
	pub password: String,
	pub gender: String,
	pub salt: String,
	pub login_failure: i32,
	pub address: String,
	pub phone: String,
	pub description: String,
	pub photo: String,
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UserQueryRequest {
	pub id: Option<String>,
	pub user_name: Option<String>,
	pub active: Option<String>,
	pub first_name: Option<String>,
	pub last_name: Option<String>,
	pub email: Option<String>,
	pub isadmin: Option<String>,
	pub gender: Option<String>,
	pub address: Option<String>,
	pub phone: Option<String>,
	pub description: Option<String>,
	pub photo: Option<String>,
	pub page:Option<i32>,
	pub rows:Option<i32>,
}

impl<'c> FromRow<'c, MySqlRow> for UserBean {
	fn from_row(row: &MySqlRow) -> Result<Self, Error> {
		Ok(UserBean {
			id: row.get(0),
			user_name: row.get(1),
			active: row.get(2),
			first_name:get_string(row.get(3)),
			last_name:get_string(row.get(4)),
			email:get_string(row.get(5)),
			isadmin:get_bool(row.get(6)),
			password:get_string(row.get(7)),
			gender:get_string(row.get(8)),
			salt:get_string(row.get(9)),
			login_failure:get_i32(row.get(10)),
			address:get_string(row.get(11)),
			phone:get_string(row.get(12)),
			description:get_string(row.get(13)),
			photo:get_image_base64(row.get(14)),
		})
	}
}

impl PartialEq for UserBean {
	fn eq(&self, other: &Self) -> bool {
		self.id == other.id
	}
}
